package com.dream.inspect.modules.app.controller.common;


import com.dream.inspect.common.utils.BeanUtilsEx;
import com.dream.inspect.common.utils.SendMessageUtil;
import com.dream.inspect.common.utils.StringUtil;
import com.dream.inspect.modules.business.entity.Datas;
import com.dream.inspect.modules.business.entity.SendMessageEntity;
import com.dream.inspect.modules.business.service.SendMessageService;
import com.dream.inspect.modules.sys.dao.SysDeptDao;
import com.dream.inspect.modules.sys.entity.SysDeptEntity;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 公共的方法
 *
 * @author admin
 */
@Controller
public class CommonMethod {

    @Autowired
    private SysDeptDao sysDeptDao;
	@Autowired
	SendMessageService sendMessageService;

    /**
     *查询当前用户所在单位拥有哪些角色
     * @param userId
     * @param roleId
     * @return
     */
    public String getRolesBy(String userId,String roleId){
        if(StringUtil.isNotEmpty(userId)){

        }else{
            switch (roleId){
                case "1": return ";1;";
                case "3":
                case "4": return ";3;4;";
                case "6":
                case "7": return ";6;7;";
                case "8":
                case "9": return ";8;9;";
                case "10": return ";10;";
            }
        }

        return  "";
    }

    /**
     * 通过部门Id 查询当前部门拥有所有角色Id
     * @param deptId
     * @return
     */
    public String getRolesByDeptId(String deptId){
        if(StringUtil.isNotEmpty(deptId)){
            SysDeptEntity s = sysDeptDao.selectById(deptId);
            String level = s.getLevel();
            switch (level){
                case "1": return ";1;";
                case "2": return ";3;4;";
                case "3": return ";6;7;";
                case "4": return ";8;9;";
            }
        }
        return  "";
    }

    /**
     * 通过部门Id 查询当前部门拥有所有A角色Id
     * @param deptId
     * @return
     */
    public String getRolesAByDeptId(String deptId){
        if(StringUtil.isNotEmpty(deptId)){
            String level = sysDeptDao.selectById(deptId).getLevel();
            switch (level){
                case "1": return ";1;";
                case "2": return ";3;";
                case "3": return ";6;";
                case "4": return ";8;";
            }
        }
        return  "";
    }

    /**
     * 通过部门Id 查询当前部门拥有所有B角色Id
     * @param deptId
     * @return
     */
    public String getRolesBByDeptId(String deptId){
        if(StringUtil.isNotEmpty(deptId)){
            String level = sysDeptDao.selectById(deptId).getLevel();
            switch (level){
                case "1": return ";1;";
                case "2": return ";4;";
                case "3": return ";7;";
                case "4": return ";9;";
            }
        }
        return  "";
    }
    
    public String getMsgInfo(String czzt) {
    	//(1：申请立项，2：取消立项，3：通过立项，4：驳回立项，5：执行立项，6：申请办结，7：取消办结，8：通过办结，9：驳回办结)
    	String msg = "";
    	switch (czzt) {
    	case "1" : 
    		msg = "申请立项";
    		break;
    	case "2" :
    		msg = "取消立项";
    		break;
    	case "3" :
    		msg = "通过立项";
    		break;
    	case "4" :
    		msg = "驳回立项";
    		break;
    	case "5" :
    		msg = "执行立项";
    		break;
    	case "6" :
    		msg = "申请办结";
    		break;
    	case "7" :
    		msg = "取消办结";
    		break;
    	case "8" :
    		msg = "通过办结";
    		break;
    	case "9" :
    		msg = "驳回办结";
    		break; 
    	}
    	return msg;
    }
    
	/**
	 * 推送消息到第三方库
	 * 
	 * @param datas
	 * @param yid
	 */
	public void sendMessageAndInsertMessage(Datas datas, String yid) {
		Date utilDate= new Date();      
		Datas result = SendMessageUtil.sendMessage(datas);
		if (result != null) {
			String type = result.getType();
			if (!"10000".equals(type)) {
				SendMessageEntity smEntity = new SendMessageEntity();
				//copy data
				BeanUtilsEx.copyProperties(smEntity, datas);
				smEntity.setCreateTime(utilDate);
				smEntity.setUpdateTime(utilDate);
				sendMessageService.insert(smEntity);
			}
			
		}
		
	}

}
